package map01;

import java.util.HashMap;

/**
 * HashMap中的方法:
 *  key的特点:
 *    HashMap保证key唯一,底层原理依然是通过hashCode()方法与equals()方法保证key的唯一
 *    HashMap中存取的键值对顺序和取出顺序不一定相同
 *
 *  V put(K key, V value)
 *          将键值对添加到map中
 *          返回值:
 *             如果当前添加的key与map中已有key都不相同,那么这个put方法返回null
 *             如果当前添加的key与map中已有key重复,此时会将key对应的新值覆盖掉老值,同时返回老值
 *
 *
 *   V get(Object key)
 *           根据key获取对应的value
 *           如果key在当前map不存在,那么返回null
 *   int size()
 *            返回的是当前map中键值对的个数
 *   void clear()
 *           清空map中所有的键值对
 */
public class HashMapDemo01 {
    public static void main(String[] args) {
        //method01();
        //method02();
        //method03();
        //method04();
        HashMap<Integer, String> hm = new HashMap<>();
        hm.put(42, "abc");
        hm.put(52, "def");
        hm.put(1, "ghk");
        System.out.println(hm);//{1=ghk, 52=def, 42=abc}
        hm.clear();
        System.out.println(hm);//{}
    }

    private static void method04() {
        HashMap<Integer, String> hm = new HashMap<>();
        hm.put(42, "abc");
        hm.put(52, "def");
        hm.put(1, "ghk");
        System.out.println(hm.size());//3
    }

    private static void method03() {
        HashMap<Integer, String> hm = new HashMap<>();
        hm.put(42, "abc");
        hm.put(52, "def");
        hm.put(1, "ghk");

        String s = hm.get(52);
        System.out.println(s);//def

        String s2 = hm.get(63);
        System.out.println(s2);//null
    }

    private static void method02() {
        HashMap<Integer, String> hm = new HashMap<>();
        hm.put(42, "abc");
        hm.put(52, "def");

        String str = hm.put(1, "ghk");
        System.out.println(str);//null
        System.out.println(hm);

        String str2 = hm.put(1, "mn");
        System.out.println(str2);//ghk
        System.out.println(hm);
    }

    private static void method01() {
        HashMap<Integer, String> hm = new HashMap<>();
        hm.put(42, "abc");
        hm.put(52, "def");
        hm.put(1, "ghk");
        System.out.println(hm);//{1=ghk, 52=def, 42=abc}
    }
}
